package client;
import java.io.* ;
import java.net.* ;
import java.util.* ;

final public class tcp_request implements Runnable {
    final static String CRLF = "\r\n";
    
    Socket socket;
    ClientView clientView;

    public tcp_request() {
    }


    // Constructor
    public tcp_request(Socket socket, ClientView clientView) throws Exception {
	this.socket = socket;
        this.clientView = clientView;

    }

    // Implement the run() method of the Runnable interface.
    public void run() {
	
        try {
	    processRequest();
            
	} catch (Exception e) {
	    System.out.println(e);
	}
       
    }
    public void print (Hashtable ht) {
        Enumeration enum1 = ht.keys ();
        while (enum1.hasMoreElements ()) {
            String key = (String) enum1.nextElement ();
            String value = (String) ht.get (key);
            System.out.println ("{ " + key + ", " + value + " }");
        }
    } 
    private void processRequest() throws Exception {
	// Get a reference to the socket's input and output streams.
	InputStream is = socket.getInputStream();
	DataOutputStream os = new DataOutputStream(socket.getOutputStream());

	// Set up input stream filters.
	BufferedReader br = new BufferedReader(new InputStreamReader(is));

        // Get the request line of the HTTP request message.
        String message = br.readLine();
        System.out.println("Message Recieved: " + message);
        String[] msg_array = message.split(";");
        Hashtable msg_hashed = new Hashtable();
        for(String i :msg_array){
            String[] row = i.split(":");
            
            msg_hashed.put(row[0], row[1]);
        }
       // print(msg_hashed);
        

        
        clientView.process_message(msg_hashed);
        // Close streams and socket.
        os.close();
        br.close();
        socket.close();

    }
}
